package com.geeklan.smart.ui.pet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.geeklan.smart.R;
import com.geeklan.smart.utils.DateUtils;
import com.geeklan.smart.widget.ItemOnClickListener;
import com.geeklan.smart.data.model.PetInfo;
import com.geeklan.smart.databinding.LayoutPetListItemBinding;

import java.util.List;

public class PetListAdapter extends RecyclerView.Adapter<PetListAdapter.ItemViewHolder> {
    private Context mContext;
    private List<PetInfo> itemList;
    private ItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public PetListAdapter(Context context, List<PetInfo> itemList) {
        this.itemList = itemList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PetListAdapter.ItemViewHolder(LayoutPetListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        PetInfo petInfo = itemList.get(position);
        holder.mBinding.tvPetName.setText(petInfo.getName());
        Glide.with(mContext).load(petInfo.getPic()).apply(RequestOptions.circleCropTransform()).into(holder.mBinding.ivPetHead);
        if(petInfo.getSex() == 1 || petInfo.getSex() == 3){
            holder.mBinding.ivPetSex.setImageResource(R.drawable.ic_gg);
        }else {
            holder.mBinding.ivPetSex.setImageResource(R.drawable.ic_mm);
        }
        holder.mBinding.tvPetInfo.setText(String.format("%s 丨 %s 丨 %skg",petInfo.getVariety(), DateUtils.calculateAge(petInfo.getBirthday()),String.format("%.1f",Float.parseFloat(petInfo.getWeight()))));
        holder.mBinding.getRoot().setOnClickListener(v -> {
            itemOnClickListener.OnItemClick(v,position);
        });
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        LayoutPetListItemBinding mBinding;

        public ItemViewHolder(@NonNull LayoutPetListItemBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }
    }
}